/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testingP;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

/**
 *
 * @author admin
 */


public class TableColumnRemove 
{

DefaultTableModel model;
JTable table;

public static void main(String[] args) 
{
new TableColumnRemove();
}

public TableColumnRemove() 
{
    JFrame frame = new JFrame("Remove a column from a JTable");
    JPanel panel = new JPanel();
    String data[][] = {{"Vinod", "MCA", "Computer"},
            {"Deepak", "PGDCA", "History"},
            {"Ranjan", "M.SC.", "Biology"},
            {"Radha", "BCA", "Computer"}};
    String col[] = {"Name.", "Course", "Subject"};
    
    model = new DefaultTableModel(data, col);
    table = new JTable(model);
    
    JTableHeader header = table.getTableHeader();
    
    header.setBackground(Color.yellow);
    JScrollPane pane = new JScrollPane(table);
    
    panel.add(pane);
    frame.add(panel);
    frame.setSize(500, 150);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    int columnCount = table.getColumnCount();
    for (int i = 0; i < columnCount; i = 0) { // here im remove columns
    Remove(table, i);
    columnCount = table.getColumnCount();
}
model.addColumn("new Field"); //after add new column

}

private void Remove(JTable table, int col_index) {
TableColumn tcol = table.getColumnModel().getColumn(col_index);
table.removeColumn(tcol);

}
}